﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.Module.Club
{
    public class Club_SportsMan : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string type = context.Request.QueryString["Type"];
            string cIdString = context.Request.QueryString["ClubId"];

            if (!string.IsNullOrEmpty(cIdString))
            {
                int cId = int.Parse(cIdString);
                Jy123.BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
                Jy123.Model.Club_Main club = BLLClub.GetModel(cId);
                Jy123.BLL.Club_StarRecord BLLRecord = new Jy123.BLL.Club_StarRecord();
                Jy123.BLL.Club.CModule BLLCModule = new Jy123.BLL.Club.CModule();
                List<Jy123.Model.Club_StarRecord> listRecord = BLLCModule.GetClubSportsMan(cId);
                if (type.Equals("Load"))
                {
                    Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
                    Jy123.Model.Public_Level level = BLLLevel.GetModel(club.FLevel.Value);
                    string html = context.Cache["clubsportsman"].ToString();
                    html = html.Replace("$ClubId$", cIdString);
                    string strAction = string.Format("/Module/Club/Club_SportsMan.ashx?Type=Save&ClubId={0}", club.ID);
                    html = html.Replace("$Action$", strAction);
                    StringBuilder sbString = new StringBuilder();
                    foreach (Jy123.Model.Club_StarRecord record in listRecord)
                    {
                        string strStar = context.Cache["clubsportsmanul"].ToString();
                        strStar = strStar.Replace("$NumberId$", "number" + record.ID);
                        string number = string.Empty;
                        if (record.Number.HasValue)
                        {
                            number = record.Number.Value.ToString();
                        }
                        strStar = strStar.Replace("$Number$", number);
                        strStar = strStar.Replace("$NameId$", "name" + record.ID);
                        strStar = strStar.Replace("$Name$", record.Name);
                        int abilty = 0;
                        abilty = level.Moral.Value + level.PhysicalFitness.Value + level.Skill.Value + level.Liveness.Value;
                        strStar = strStar.Replace("$Ability$", abilty.ToString());
                        strStar = strStar.Replace("$PositionId$", "position" + record.ID);
                        strStar = strStar.Replace("$Position$", record.Position);
                        sbString.Append(strStar);
                    }
                    html = html.Replace("$Ul$", sbString.ToString());
                    context.Response.Write(html);
                }
                else
                {
                    //Save
                    foreach (Jy123.Model.Club_StarRecord record in listRecord)
                    {
                        int number = 0;
                        string strId = record.ID.ToString();
                        string strNumber = context.Request.Form["number" + strId].ToString();
                        if (!string.IsNullOrEmpty(strNumber))
                        {
                            number = int.Parse(strNumber);
                        }
                        if (number > 0)
                        {
                            record.Number = number;
                        }
                        else
                        {
                            record.Number = null;
                        }
                        record.Name = context.Request.Form["name" + strId].ToString();
                        record.Position = context.Request.Form["position" + strId].ToString();
                        BLLRecord.Update(record);//需要改进
                    }
                    string strRedirect = string.Format("/Module/Club/ClubInfo.aspx?Type=Load&ClubId={0}&Step=4", club.ID);
                    context.Response.Redirect(strRedirect);
                }
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
